package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.client.MovieClientProduct;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.domain.Movie;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.dto.MovieDto;
import com.woniuxy.entity.MovieElasticDemo;
import com.woniuxy.param.MovieParam;
import com.woniuxy.service.MovieService;
import com.woniuxy.util.BeanCopyUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private MovieClientProduct movieClientProduct;
    @Autowired
    private ElasticsearchRestTemplate template;

    //查询电影服务是否上架,如果可以就上架 0为未上架 1为上架
    @GetMapping("/change")
    public JsonResultDto changeMovieUp(){
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.eq("m_status","0");
        List<Movie> list = movieService.list(wrapper);
        for(Movie movie:list){
            movie.setmStatus("1");
            movieService.updateById(movie);
            redisConfig.opsForHash().put("Movie",movie.getmId(),movie);
        }
        return new JsonResultDto("1000","successful",null,null);
    }
    @GetMapping("/selectMovie")
    public JsonResultDto selectMovie(MovieParam movieParam){
        List<Movie> movies = movieService.selectMovie(movieParam);
        List<MovieDto> movieDtos = BeanCopyUtil.copyListPropertie(movies, MovieDto::new);
        return  new JsonResultDto("1000","successful",null,movieDtos);
    }
    @GetMapping("/selectMovieByMid")
    public Movie selectMovieByMid(int mId){
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.eq("m_id",mId);
        return movieService.getOne(wrapper);
    }
    @GlobalTransactional(rollbackFor = Exception.class)
    @GetMapping("/test")
    public JsonResultDto test(){
        Movie movie = new Movie();
        movie.setmStatus("0");
        movieService.save(movie);
        JsonResultDto jsonResultDto = movieClientProduct.productTest();
        if(jsonResultDto.getCode().equals("500"))throw new RuntimeException("代码有问题");
        return null;
    }
    @PostMapping("/putAway")//查询数据库状态为1的为上架的电影,放入Elasticsearch
    public JsonResultDto putAway(){
        return movieService.putAway();
    }

    @GetMapping("/searchMovie")
    public SearchHits<MovieElasticDemo> searchMovie(String value){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        NativeSearchQuery query = builder.withQuery(new MatchQueryBuilder("movieElasticSearch", value)).build();
        SearchHits<MovieElasticDemo> search = template.search(query, MovieElasticDemo.class);
        return search;
    }


}

